<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 1、导入mapbox的依赖 -->
    <script src='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.js'></script>
    <link href='https://api.mapbox.com/mapbox-gl-js/v2.15.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        * {
            margin: 0;
            padding: 0
        }

        #map {
            width: 100vw;
            height: 100vh;
        }

        .btns button {
            position: fixed;
            z-index: 1000;
            top: 20px;
            width: 100px;
            height: 40px;
        }

        .big {
            left: 50px;
        }

        .small {
            left: 300px;
        }
    </style>
</head>

<body>
    <div class="btns">
        <button class="big" onclick="addMap()">放大</button>
        <button class="small" onclick="reduceMap()">缩小</button>
    </div>

    <!-- 2、设置地图容器的挂载点 -->
    <div id="map">

    </div>
    <script>
        /* token */
        mapboxgl.accessToken = 'pk.eyJ1IjoiZXJpYzA5MTciLCJhIjoiY2x3cTI2Zzg0MWY2dDJqcWVpdDI1MHNpcCJ9.k9gLW7ZgLyISNdYHIEEGdQ'
        const map = new mapboxgl.Map({
            /* target-ol */
            container: 'map', // container ID
            /* style-layers */
            style: 'mapbox://styles/mapbox/streets-v12', // style URL
            center: [114.30, 30.50], // starting position [lng, lat]
            zoom: 1, // starting zoom
        });
        map.on("style.load", () => {
            /* 加载地图控件 */
            map.addControl(new mapboxgl.FullscreenControl());
            const nav = new mapboxgl.NavigationControl(
                {
                    //是否显示指南针按钮，默认为true
                    "showCompass": true,
                    //是否显示缩放按钮，默认为true
                    "showZoom": true
                }
            );
            //添加导航控件，控件的位置包括'top-left', 'top-right','bottom-left' ,'bottom-right'四种，默认为'top-right'
            map.addControl(nav, 'top-left');
        })
        function addMap() {
            let zoom = map.getZoom();
            map.setZoom(zoom + 1)
        }
        function reduceMap() {
            let zoom = map.getZoom();
            map.setZoom(zoom - 1)
        }
    </script>
</body>

</html>